cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0042 NEW)
set (CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

project (zorm)
set(VERSION 1.0.0.0)
SET(CMAKE_CXX_FLAGS "-D_GLIBCXX_USE_CXX17_ABI=0")
# set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
include_directories(./thirds/sqlit3)
link_directories(./thirds/sqlit3/lib)
include_directories(./thirds/zorm/include)
include_directories(./thirds/zjson)
include_directories(./thirds/MySQL-C-Api-6.1/include)
if(MSVC)
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
    add_definitions(-D__MSVC__)
    link_directories(./thirds/MySQL-C-Api-6.1/lib)
    include_directories(./thirds/pgsql/include)
    link_directories(./thirds/pgsql/lib)
    include_directories(./thirds/dm8/dpi/include)
    link_directories(./thirds/dm8/dpi)
    set(MysqlDll libmysql.dll)
    set(pqName libpq.dll)
    set(sqliteName sqlite3.dll)
    set(dm8Name dmdpi.dll)
elseif(APPLE)
    link_directories(./thirds/MySQL-C-Api-6.1/lib)
    include_directories(./thirds/pgsql/macos/include)
    link_directories(./thirds/pgsql/macos/lib)
    include_directories(/opt/dm8/dpi/include)
    link_directories(/opt/dm8/dpi)
    set(MysqlDll libmysqlclient.a)
    set(pqName libpq.a)
    set(dm8Name libdmdpi.a)
elseif(UNIX)
    add_definitions(-D__LINUX__)
    add_definitions(-D_GNU_SOURCE)
    # find_package(Threads REQUIRED)
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")

    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")

    include_directories(/usr/include/postgresql)
    include_directories(/opt/dm8/dpi/include)
    link_directories(/opt/dm8/dpi)
    set(dlLinkParam dl)
    set(MysqlDll mysqlclient)
    # set(pthread pthread)
    set(pqName pq)
    set(sqliteName libsqlite3.so)
    set(dm8Name libdmdpi.so)
endif()

add_definitions(-DZORM_LIB)
add_definitions(-DSQLITE_OMIT_TEMPDB)

set(NODE_LINK_LIBS "")
set(NODE_EXTERNAL_INCLUDES "")

FILE(GLOB_RECURSE SOURCE_FILES "./addon/*.cc") 
FILE(GLOB_RECURSE HEADER_FILES "./addon/*.h") 

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
message("-------- CMAKE_JS_INC -------" ${CMAKE_JS_INC})
# Include Node-API wrappers
target_include_directories(${PROJECT_NAME} PRIVATE  
    ${CMAKE_SOURCE_DIR}/node_modules/node-addon-api 
    ${CMAKE_SOURCE_DIR}/node_modules/node-addon-api/src
    ${CMAKE_JS_INC})
#target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB})
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} ${MysqlDll} ${pqName} ${sqliteName} ${dm8Name} ${dlLinkParam})
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)

# define NPI_VERSION
add_definitions(-DNAPI_VERSION=8)
add_definitions(-DNAPI_DISABLE_CPP_EXCEPTIONS)
